<?php
class baseAction extends Action{
	private $v;
	
	public function __construct(){

$app_key = "12587554";
/*$secret="744986c5b0db2bd869c4f22e9ea2b114";
$timestamp=time()."000";
$message = $secret.'app_key'.$app_key.'timestamp'.$timestamp.$secret;
$mysign=strtoupper(hash_hmac("md5",$message,$secret));
setcookie("timestamp",$timestamp);
setcookie("sign",$mysign);
*/
//解决页面间链接问题
		//print_r( $_GET);
		if(isset($_REQUEST['top_parameters'])){
			setcookie('top_parameters',$_REQUEST['top_parameters']);	
		}else{
			$_REQUEST['top_parameters'] = $_COOKIE['top_parameters'];
		}
		
		if(@$_REQUEST['encode']=="utf-8"){
			$top_parameters=base64_decode($_REQUEST['top_parameters']);
		}else{
			$top_parameters=mb_convert_encoding(base64_decode(@$_REQUEST['top_parameters']),"UTF-8","GBK");
		}

		parse_str($top_parameters,$parameters);
		//print_r($parameters);
		if($parameters['visitor_nick']){
			//global $userName;
			$GLOBALS['userName'] = $parameters['visitor_nick'];
			//print_r($globals);
			$userName = $GLOBALS['userName'];
			
			$p = D("ju_users");
			$u = $p->where("user_name='".$userName."'")->find();
			
			if($u['access_token']!=''){
				$points = $u['points'];
			}else{
				if($_GET['code']!=''){
					//获取access_token;
					 $code = $_REQUEST['code'];   //通过访问https://oauth.taobao.com/authorize获取code
					 $grant_type = 'authorization_code';
					 $redirect_uri = 'http://jie.wantujie.com';  //此处回调url要和后台设置的回调url相同
					 $client_id = C('APP_KEY');//自己的APPKEY
			 		$client_secret = C('APP_SECRET');//自己的appsecret
					 //请求参数
					 $postfields= array( 'grant_type'     => $grant_type,
										 'client_id'     => $client_id,
										 'client_secret' => $client_secret,
										 'code'          => $code,
										 'redirect_uri'  => $redirect_uri
					 );
					 
					 $url = 'https://oauth.taobao.com/token';
					 try{
						$token = json_decode(curl($url,$postfields));
					}catch(Exception $e){
						echo "出错了！！！！";
						return ;
					}
					$access_token = $token->access_token;
					// echo $access_token;
					$users = D("ju_users");
					$u = $users->where("user_name='".$userName."'")->find();
					if(!$u){
						$newUser = array(
							"user_name"=>$userName,
							"points"=>0,
							"access_token"=>$access_token
						);
						$users->add($newUser);
					}
					//获取买家信息；
					//
					$c = $this->taobao_client();
					$req = $c->load_api('UserBuyerGetRequest');
					$req->setFields("nick,avatar");
					$resp = $c->execute($req, $access_token);
					$resp = object_array($resp);
					
					
					$id = $users->where("user_name='".$userName."'")->find();
					$points = $id['points'];
					$id = $id['id'];
					$data['id'] = $id;
					$data['imgs'] = $resp['user']['avatar'];
					$data['user_name'] = $resp['user']['nick'];
					$data['access_token'] = $token->access_token;
					
					//print_r($data);
					$users->create();
					$users->save($data);
					//echo $users->getLastSql();
				}else{
					$authUrl =	'https://oauth.taobao.com/authorize?client_id=12587554&response_type=code&redirect_uri=http://jie.wantujie.com';
					echo '<meta http-equiv="refresh" content="0;url='.$authUrl.'">';
					return;
				}
				//header('Location: http://www.example.com/');
			}
		$this->assign("helloStr","欢迎您，".$userName."，当前积分：<b class=\"points\">".$points."</b>");
		}else{
			$userName = "";
			
			$this->assign("helloStr","<a href='https://login.taobao.com//member/login.jhtml?spm=a1z04.1.0.2.Xg08o7&f=top&redirectURL=http%3A%2F%2Fyingyong.taobao.com%2Fshow.htm%3Fspm%3Da1z04.4.1000553.2.shQ6Jk%26app_id%3D6173001'>请登录</a>");
		}

		$nav = D("ju_class");
		$class = $nav->where("p_id=0")->order("sort desc")->select();
		$this->assign("class",$class);
		$this->assign("classId",$_GET['classId']);
				
		$this->assign("userName",$userName);
		$this->assign("points",$points);
		$this->assign("app_key",C('APP_KEY'));
		
	}
	
	
	public function taobao_client()
	{
		vendor('Taobaotop.TopClient');
		vendor('Taobaotop.RequestCheckUtil');
		vendor('Taobaotop.Logger');
		$tb_top = new TopClient;
		$tb_top->appkey = '12587554';
		$tb_top->secretKey = '744986c5b0db2bd869c4f22e9ea2b114';
		return $tb_top;
	}
	
	function mysubstr($str, $start, $len) {
		$tmpstr = "";
		$strlen = $start + $len;
		for($i = 0; $i < $strlen; $i++) {
			if(ord(substr($str, $i, 1)) > 0xa0) {
				$tmpstr .= substr($str, $i, 2);
				$i++;
			} else
				$tmpstr .= substr($str, $i, 1);
		}
		return $tmpstr;
	}
}

?>